// *****************加载获取数据

// function getDate() {
//     let date = new Date();
//     let year = date.getFullYear();
//     let month = date.getMonth() + 1;
//     let day = date.getDay();

//     let hour = date.getHours();
//     let min = date.getMinutes();
//     let second = date.getSeconds();
//     return year + '-' + month + '-' + day + '' + hour + ':' + min + ':' + second;
// }




function getList() {
    $('tbody').empty();
    $.ajax({
        url: "/admin/comments",
        type: "get",
        success: function (res) {
            // console.log(res);
            let str = "";
            if (res.status === 0) {

                res.data.forEach(item => {
                    str += `
                    <tr>
            <td>${item.id}</td>
            <td>${item.uname}</td>
            <td>${item.content}</td>
            <td>${item.cdate}</td>
            <td>
              
              <button data-id="${item.id}" type="button" class="layui-btn layui-btn-xs layui-btn-danger delete">
                删除
              </button>
            </td>
          </tr>
                    `

                });

                $('tbody').html(str);

            }
        }

    })
}

getList();




// 删除评论

// 给删除按钮注册点击事件

let layer = layui.layer;
$('tbody').on("click", ".delete", function () {
    layer.confirm('你确定要删除吗?', index => {
        // 获取点击对应的id
        let id = $(this).attr('data-id');

        // 发送请求
        $.ajax({
            url: "/admin/comments/" + id,
            type: "DELETE",
            success: function (res) {
                layer.msg(res.message);
                if (res.status === 0) {
                    layer.close(index);
                    getList();
                }
            }
        })


    });
})